package sin;

public class Main {

	public static void main(String[] args) {
		System.out.println("Begin testing!!");
		withExecutorOrWithout();
		System.out.println("--------");
		findOptimParams(new SimpleCount());
	}
	
	public static void withExecutorOrWithout() {
		SumCountable with = new CountWithExecutorService();
		SumCountable without = new SimpleCount();
		
		int nThreads = 3;
		int n = 1000;
		
		long timeWith = System.nanoTime();
		double answerWith = with.sumcount(nThreads, n);
		timeWith = System.nanoTime() - timeWith;
		
		long timeWithout = System.nanoTime();
		double answerWithout = without.sumcount(nThreads, n);
		timeWith = System.nanoTime() - timeWithout;
		
		System.out.println("Answer equals: "+(answerWith==answerWithout));
		System.out.println("Win: "+((timeWith<timeWithout)?"Executor Service":"SimpleCount")); 
	}

	public static void findOptimParams(SumCountable counter) {
		int[] lengths = {1000};
		int[] nThreads = {1,2,4,8,16};
		
		long bestResult=Long.MAX_VALUE;
		int bestLength=lengths[0];
		int bestNThreads=nThreads[0];
		
		for (int n:lengths) {
			for (int nThread:nThreads) {
				long time = System.nanoTime();
				counter.sumcount(nThread, n);
				time = System.nanoTime() - time;
				if (time<bestResult) {
					bestResult = time;
					bestLength = n;
					bestNThreads = nThread; 
				}
			}
		}
		
		System.out.printf("Best result: %d on Len: %d on nThreads %d",bestResult, bestLength, bestNThreads);
	}
}
